"""
课程安排模型
"""
from sqlalchemy import Column, String, Integer, Numeric, Enum, ForeignKey, DateTime, func
from sqlalchemy.orm import relationship
from .base import Base


class CourseSchedule(Base):
    """课程安排表"""
    __tablename__ = "course_schedules"
    
    course_id = Column(Integer, ForeignKey("courses.id"), nullable=False, comment="课程ID")
    stage_number = Column(Integer, nullable=False, comment="阶段序号")
    teacher_id = Column(Integer, ForeignKey("employees.id"), nullable=False, comment="讲师ID")
    class_name = Column(String(100), comment="班级名称")
    start_date = Column(DateTime, nullable=False, comment="开始日期")
    end_date = Column(DateTime, nullable=False, comment="结束日期")
    class_time = Column(String(50), comment="上课时间")
    classroom = Column(String(50), comment="教室")
    max_students = Column(Integer, default=30, comment="最大学员数")
    current_students = Column(Integer, default=0, comment="当前学员数")
    status = Column(Enum("scheduled", "ongoing", "completed", "cancelled", name="schedule_status"), 
                   default="scheduled", comment="状态")
    
    # 关系
    course = relationship("Course", back_populates="course_schedules")
    teacher = relationship("Employee", back_populates="course_schedules")
    student_schedules = relationship("StudentCourseSchedule", back_populates="schedule")
    
    def __repr__(self):
        return f"<CourseSchedule(id={self.id}, course_id={self.course_id}, class_name='{self.class_name}')>"


class StudentCourseSchedule(Base):
    """学员课程安排关系表"""
    __tablename__ = "student_course_schedules"
    
    student_id = Column(Integer, ForeignKey("students.id"), nullable=False, comment="学员ID")
    schedule_id = Column(Integer, ForeignKey("course_schedules.id"), nullable=False, comment="课程安排ID")
    enrollment_date = Column(DateTime, nullable=False, comment="入学日期")
    status = Column(Enum("enrolled", "completed", "dropped", "suspended", name="enrollment_status"), 
                   default="enrolled", comment="状态")
    attendance_rate = Column(Numeric(5, 2), default=0.00, comment="出勤率")
    
    # 关系
    student = relationship("Student", back_populates="course_schedules")
    schedule = relationship("CourseSchedule", back_populates="student_schedules")
    
    def __repr__(self):
        return f"<StudentCourseSchedule(id={self.id}, student_id={self.student_id}, schedule_id={self.schedule_id})>"
